/**
 * @file	sensor.h
 * @author	Mitch, David
 * @brief
 */


#ifndef SENSOR_H_
#define SENSOR_H_


typedef enum _ir_dir
{
	IR_NORTH,
	IR_EAST,
	IR_SOUTH,
	IR_WEST,
	IR_NONE,
} IR_Direction_t;

/** Get the North sensor reading.
 * @return 1 if sensor detects an IR signal, 0 if it does not.
 */
uint8_t get_IR_North()
{
	// if the N pin is low, then sensor detects an IR signal
	return (PINA & _BV(PORTA7)) == 0;
}

/** Get the East sensor reading.
 * @return 1 if sensor detects an IR signal, 0 if it does not.
 */
uint8_t get_IR_East()
{
	// if the E pin is low, then sensor detects an IR signal
	return (PINA & _BV(PORTA5)) == 0;
}

/** Get the South sensor reading.
 * @return 1 if sensor detects an IR signal, 0 if it does not.
 */
uint8_t get_IR_South()
{
	// if the S pin is low, then sensor detects an IR signal
	return (PINA & _BV(PORTA3)) == 0;
}

/** Get the South sensor reading.
 * @return 1 if sensor detects an IR signal, 0 if it does not.
 */
uint8_t get_IR_West()
{
	// if the W pin is low, then sensor detects an IR signal
	return (PINA & _BV(PORTA1)) == 0;
}

IR_Direction_t get_current_direction()
{
	// according to the Pololu documentation, only one line should
	// be high at a time.
	uint8_t input_reg = PINA;
	if ((input_reg & _BV(PORTA1)) == 0) return IR_WEST;
	if ((input_reg & _BV(PORTA3)) == 0) return IR_SOUTH;
	if ((input_reg & _BV(PORTA5)) == 0) return IR_EAST;
	if ((input_reg & _BV(PORTA7)) == 0) return IR_NORTH;

	return IR_NONE;
}

#endif /* SENSOR_H_ */
